//
//  AppDelegate.m
//  SuperNews
//
//  Created by qiang on 16/1/8.
//  Copyright © 2016年 qiang. All rights reserved.
//

#import "AppDelegate.h"
#import "NewPageViewController.h"
#import "ReadPageViewController.h"
#import "ListenPageViewController.h"
#import "TalkPageViewController.h"
#import "MePageViewController.h"
#import "PlayerViewController.h"
#import "MusicModel.h"

@interface AppDelegate ()

@property (nonatomic, strong) UILabel *statusLabel;

@end
@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
//    [self.window setWindowLevel:UIWindowLevelStatusBar + 1];
    [self.window makeKeyAndVisible];
    
    //后台播放音频设置
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setActive:YES error:nil];
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    
    
    NewPageViewController *newPageVC = [[NewPageViewController alloc] init];
    UINavigationController *newPageNC = [[UINavigationController alloc] initWithRootViewController:newPageVC];
    [newPageNC.navigationBar setBackgroundImage:[UIImage imageNamed:@"top_navigation_background"] forBarMetrics:UIBarMetricsDefault];
    
    ReadPageViewController *readPageVC = [[ReadPageViewController alloc] init];
    UINavigationController *readPageNC = [[UINavigationController alloc] initWithRootViewController:readPageVC];
    [readPageNC.navigationBar setBackgroundImage:[UIImage imageNamed:@"top_navigation_background"] forBarMetrics:UIBarMetricsDefault];
    
    ListenPageViewController *listenPageVC = [[ListenPageViewController alloc] init];
    UINavigationController *listenPageNC = [[UINavigationController alloc] initWithRootViewController:listenPageVC];
    [listenPageNC.navigationBar setBackgroundImage:[UIImage imageNamed:@"top_navigation_background"] forBarMetrics:UIBarMetricsDefault];
    
    
    TalkPageViewController *talePageVC = [[TalkPageViewController alloc] init];
    UINavigationController *talkPageNC = [[UINavigationController alloc] initWithRootViewController:talePageVC];
    [talkPageNC.navigationBar setBackgroundImage:[UIImage imageNamed:@"top_navigation_background"] forBarMetrics:UIBarMetricsDefault];
    
    MePageViewController *mePageVC = [[MePageViewController alloc] init];
    
    
    newPageNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"新闻" image:[UIImage imageNamed:@"tabbar_icon_news_normal"] selectedImage:[UIImage imageNamed:@"tabbar_icon_news_highlight"]];
    readPageNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"阅读" image:[UIImage imageNamed:@"tabbar_icon_reader_normal"] selectedImage:[UIImage imageNamed:@"tabbar_icon_reader_highlight"]];
    listenPageNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"试听" image:[UIImage imageNamed:@"tabbar_icon_media_normal"] selectedImage:[UIImage imageNamed:@"tabbar_icon_media_highlight"]];
    talkPageNC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"话题" image:[UIImage imageNamed:@"tabbar_icon_bar_normal"] selectedImage:[UIImage imageNamed:@"tabbar_icon_bar_highlight"]];
    mePageVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我" image:[UIImage imageNamed:@"tabbar_icon_me_normal"] selectedImage:[UIImage imageNamed:@"tabbar_icon_me_highlight"]];
    //
    
    UITabBarController *tabBarC = [[UITabBarController alloc] init];
    tabBarC.viewControllers = [NSArray arrayWithObjects:newPageNC, readPageNC, listenPageNC, talkPageNC, mePageVC, nil];
    tabBarC.tabBar.tintColor = [UIColor colorWithRed:1.000 green:0.023 blue:0.061 alpha:1.000];
    self.window.rootViewController = tabBarC;
    
    self.statusLabel = [[UILabel alloc] initWithFrame:[UIApplication sharedApplication].statusBarFrame];
    self.statusLabel.textColor = [UIColor whiteColor];
    self.statusLabel.font = [UIFont systemFontOfSize:11.0];
    self.statusLabel.textAlignment = NSTextAlignmentCenter;
    self.statusLabel.userInteractionEnabled = YES;
    self.statusLabel.backgroundColor = [UIColor colorWithRed:150.0 / 255.0 green:10.0 / 255.0 blue:0.0 alpha:1.0];
    self.statusLabel.hidden = YES;
    [self.window addSubview:self.statusLabel];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = [UIApplication sharedApplication].statusBarFrame;
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.statusLabel addSubview:button];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playingAction) name:@"playing" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noplayingAction) name:@"noplaying" object:nil];
    
    return YES;
}

- (void)buttonAction
{
    PlayerViewController *player = [PlayerViewController sharePlayerViewController];
    [self.window.rootViewController presentViewController:player animated:YES completion:^{
    }];
}

- (void)playingAction
{
    self.statusLabel.hidden = NO;
    if (0 != [[[PlayerViewController sharePlayerViewController] listenArr] count]) {
         self.statusLabel.text = [NSString stringWithFormat:@"正在播放:%@", [[[[PlayerViewController sharePlayerViewController] listenArr] objectAtIndex:0] alt]];
    }
    [self.window setWindowLevel:UIWindowLevelStatusBar + 1];
}

- (void)noplayingAction
{
    self.statusLabel.hidden = YES;
    [self.window setWindowLevel:UIWindowLevelNormal];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    //开始接受远程事件
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    //结束接受远程事件
    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
    [self resignFirstResponder];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    // Saves changes in the application's managed object context before the application terminates.
}

@end
